package com.erpanderp.wam.usm.view.user;

import java.io.Serializable;
import java.util.TimeZone;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import com.erpanderp.wam.usm.model.entity.user.Employee;
import com.erpanderp.wam.usm.model.entity.user.EmployeeAndReporting;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.usm.model.service.user.EmployeeAndReportingDAO;
import com.erpanderp.wam.usm.model.service.user.WAMUserDAO;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.ValidationUtil;
import com.erpanderp.wam.util.WAMPasswordUtil;

@ManagedBean
@ViewScoped
public class PersonalInfoBean implements Serializable {

	private static final long serialVersionUID = 1L;
	private TimeZone timeZone = TimeZone.getDefault();
	
	HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
	private WAMUser wamUser = (WAMUser) session.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER);
	
	private WAMUserDAO wamUserDAO=new WAMUserDAO();
	private EmployeeAndReportingDAO employeeAndReportingDAO=new EmployeeAndReportingDAO();
	
	private EmployeeAndReporting employeeAndReporting=new EmployeeAndReporting();	
	private Employee employee=new Employee();

	private boolean FORM_VALID;
	private String oldPassword;
	private String newPassword;
	private String confirmPassword;
	
	public PersonalInfoBean() {
		System.out.println("wamUser.getEmployee().getEmail()=============="+wamUser.getEmployee().getEmail());
		employee = wamUserDAO.getEmployeeByEmail(wamUser.getEmployee().getEmail());
		System.out.println("employee========="+employee);
		System.out.println("GET ANME ========="+employee.getFirstName());
		employeeAndReporting=employeeAndReportingDAO.getEmployeeReportingDetail(wamUser.getEmployee().getEmployeeId());	
		System.out.println("employeeAndReporting========"+employeeAndReporting);
		System.out.println("GET PROJ NAME ======="+employeeAndReporting.getProject());
	}
	
	public void updateAction(){
		validate();
		try{
			System.out.println("oldPassword========"+oldPassword);
			System.out.println("newPassword========"+newPassword);
			System.out.println("confirmPassword========"+confirmPassword);
			
			wamUser = wamUserDAO.getPasswordByEmployeeId(wamUser.getEmployee().getEmployeeCode());
			String wamPassword = WAMPasswordUtil.decrypt(wamUser.getPassword());
			
			System.out.println("=========wamPassword==========="+wamPassword);
			
			if(FORM_VALID){
			if(oldPassword.equals(wamPassword)){	
				if(oldPassword.equals(newPassword)){
					FacesContext.getCurrentInstance().addMessage("", new FacesMessage(FacesMessage.SEVERITY_INFO, "Your current password and new password sholud not same.", ""));
				}else{
						if(newPassword.equals(confirmPassword)){					
							wamUser.setPassword(WAMPasswordUtil.encrypt(newPassword));
							wamUserDAO.updateWAMUserPassword(wamUser);					
							FacesContext.getCurrentInstance().addMessage("", new FacesMessage(FacesMessage.SEVERITY_INFO, "Your password has been changed successfully.", ""));
						}else{					
							FacesContext.getCurrentInstance().addMessage("", new FacesMessage(FacesMessage.SEVERITY_INFO, "Please reenter the new password correctly.", ""));
						}
				}
			}else{				
				FacesContext.getCurrentInstance().addMessage("", new FacesMessage(FacesMessage.SEVERITY_INFO, "You current password is incorrect.", ""));
			}		
		}
		}catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public void validate(){
		FORM_VALID = true;		
		
	   if(ValidationUtil.isEmpty(oldPassword, "Current password", null, FacesContext.getCurrentInstance()) != null){
				FORM_VALID = false;
	   }
	   if(ValidationUtil.isEmpty(newPassword, "New password", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
	   }
	   if(ValidationUtil.isEmpty(confirmPassword, "Confirm new password", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
	   }
		System.out.println("FINAL FORM_VALID============="+FORM_VALID);		
	}

	
	//SETTERS AND GETTERS
	public WAMUser getWamUser() {
		return wamUser;
	}
	public void setWamUser(WAMUser wamUser) {
		this.wamUser = wamUser;
	}


	public WAMUserDAO getWamUserDAO() {
		return wamUserDAO;
	}
	public void setWamUserDAO(WAMUserDAO wamUserDAO) {
		this.wamUserDAO = wamUserDAO;
	}

	public Employee getEmployee() {
		return employee;
	}
	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public EmployeeAndReporting getEmployeeAndReporting() {
		return employeeAndReporting;
	}
	public void setEmployeeAndReporting(EmployeeAndReporting employeeAndReporting) {
		this.employeeAndReporting = employeeAndReporting;
	}

	public EmployeeAndReportingDAO getEmployeeAndReportingDAO() {
		return employeeAndReportingDAO;
	}
	public void setEmployeeAndReportingDAO(EmployeeAndReportingDAO employeeAndReportingDAO) {
		this.employeeAndReportingDAO = employeeAndReportingDAO;
	}

	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public String getConfirmPassword() {
		return confirmPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

	public TimeZone getTimeZone() {
		return timeZone;
	}

	public void setTimeZone(TimeZone timeZone) {
		this.timeZone = timeZone;
	}

}
